//   App Tittle: Free Note
//	Institution: Arizona State University
//	 Developers: Rodrigo Rodriguez, Andy Nguyen, Candise Orr
//	       Date: 3/27/2011

#import "NoteViewController.h"


@implementation NoteViewController
@synthesize myTextView;
@synthesize noteInView;
@synthesize notesFromFile;
@synthesize ignoreDisappear;
@synthesize newNote;
@synthesize indexOfNoteRead;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad{
	
	//NSLog(@"viewDidLoad NV");
    [super viewDidLoad];
	//[self loadText:[[NSBundle mainBundle] pathForResource:@"Alice" ofType:@"txt"]];
	
	//if this is a new note
	if(newNote == YES)
	{
		self.navigationItem.title = @"New Note";
		self.myTextView.text = @"";
		
		noteInView = [[NSMutableDictionary alloc] init];
		
	}
	else //it is a previous note
	{
		self.navigationItem.title = [noteInView objectForKey:@"TITLE"];
		self.myTextView.text = [noteInView objectForKey:@"CONTENT"];
	}

}

- (void) viewWillAppear:(BOOL)animated{
	

	[super	viewWillAppear:animated];
	
	// clear the flag
	ignoreDisappear = NO;
	
	
}

- (void) viewWillDisappear:(BOOL) animate
{
	
	if(ignoreDisappear == NO) {

		//set the date
		NSDate* date = [[NSDate alloc]init];
		date = [NSDate date];
		//Create the dateformatter object
		NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
		//Set the required date format
		[formatter setDateFormat:@"MM-dd-yyyy h:mm a"];
		//Get the string date
		NSString* str = [formatter stringFromDate:date];
		//Display on the console
		
		if(([self.myTextView.text length] != 0) && (newNote == YES))
		{
			NSString* name;
			if ([myTextView.text length] >= 10) {
				name = [myTextView.text substringWithRange:(NSRange){0,10}];
			}
			else{
				name = myTextView.text;
			}
			//set the date, content, and title
			[noteInView setObject:str forKey:@"DATE"];
			[noteInView setObject:myTextView.text forKey:@"CONTENT"];
			[noteInView setObject:name forKey:@"TITLE"];
			
			notesFromFile = [[NSMutableArray alloc]init];
			notesFromFile = [Utilities readNoteFromFile];
			[notesFromFile addObject:noteInView];
			//write note to file
			[Utilities writeNoteToFile:notesFromFile];
		}
		//if it's a new note
		if(newNote == NO)
		{
			//set the new date and contents
			[noteInView setObject:str forKey:@"DATE"];
			[noteInView setObject:myTextView.text forKey:@"CONTENT"];
			
			notesFromFile = [[NSMutableArray alloc]init];
			//read note from file
			notesFromFile = [Utilities readNoteFromFile];
			//write note to file
			[notesFromFile replaceObjectAtIndex:indexOfNoteRead withObject:noteInView];			
			[Utilities writeNoteToFile:notesFromFile];
		}

		
	}
	[super viewWillDisappear:animate];
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
}

- (void)viewDidUnload {
	[self.navigationController popViewControllerAnimated:YES];
    [super viewDidUnload];
}


- (void)dealloc {
    [super dealloc];
}


@end
